package com.psp.bu.controller;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.northend.util.NumUtil;
import com.northend.util.VerifyCodeUtil;
import com.psp.bu.cache.dao.UserCacheDao;
import com.psp.bu.controller.res.ObjectResult;
import com.psp.bu.controller.res.bean.RImgCodeBean;
import com.psp.bu.controller.res.bean.RQiniuFileBean;
import com.psp.bu.controller.springmvc.req.UploadFileParam;
import com.psp.bu.controller.springmvc.req.getLoginVcodeParam;
import com.psp.bu.model.Code;
import com.psp.bu.service.QiniuService;
import com.psp.bu.service.exception.ServiceException;
import com.psp.bu.service.res.UploadUEFile;

@Component
public class FileController {

	Logger logger = Logger.getLogger(FileController.class);

	@Autowired
	QiniuService qiniuServiceImpl;

	@Autowired
	UserCacheDao userCacheImpl;

	public ObjectResult<RQiniuFileBean> uploadFile(UploadFileParam param, HttpServletRequest request,
			HttpServletResponse response) {

		ObjectResult<RQiniuFileBean> result = new ObjectResult<>();
		try {
			String media_id = param.getMedia_id();
			RQiniuFileBean file = qiniuServiceImpl.uploadFile(media_id);
			result.setData(file);
			result.setFlag(file != null);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

	public ObjectResult<RImgCodeBean> getLoginVCode(getLoginVcodeParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RImgCodeBean> result = new ObjectResult<>();
		try {
			String verifyCode = VerifyCodeUtil.generateVerifyCode(4);
			String account = param.getAccount();
			int w = NumUtil.toInt(param.getW(), 100);
			int h = NumUtil.toInt(param.getH(), 40);
			String img = VerifyCodeUtil.outputImage(w, h, verifyCode);
			String base64img = "data:image/jpeg;base64," + img.replaceAll("\r\n", "");
			if (base64img != null) {
				RImgCodeBean data = new RImgCodeBean();
				data.setKey(account);
				data.setUrl(base64img);
				Code code = userCacheImpl.getLoginCode(account);
				if (code == null) {
					code = new Code();
					code.setNum(5);
				}
				code.setCode(verifyCode);
				code.setTime(System.currentTimeMillis());
				userCacheImpl.setLoginCode(account, code);
				result.setData(data);
				result.setFlag(true);
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 七牛上传图片
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RQiniuFileBean> uploadImage(HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RQiniuFileBean> result = new ObjectResult<>();
		try {
			RQiniuFileBean file = qiniuServiceImpl.uploadImgFile(request);
			result.setData(file);
			result.setFlag(file != null);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

	public Object uploadUEImg(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> result = new HashMap<String, Object>();
		List<UploadUEFile> ds = qiniuServiceImpl.uploadUEImg(request);
		if (ds != null && ds.size() > 0) {
			Iterator<UploadUEFile> iterator = ds.iterator();
			while (iterator.hasNext()) {
				UploadUEFile file = iterator.next();
				result.put("state", file.isUpload() == true ? "SUCCESS" : "FAIL");
				result.put("original", file.getOriginalFilename());
				result.put("size", file.getFileSize());
				result.put("title", file.getFileName());
				result.put("type", file.getFileType());
				result.put("url", file.getUrl());
			}
			logger.info("富文本：" + result.toString());
		}
		return result;
	}

}
